package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.CoordinationOffice;
import com.smfx.yxt.model.CoordinationOfficeSendee;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.OfficeQueryVO;
import com.smfx.yxt.reqVo.OfficeReqVO;
import com.smfx.yxt.service.ICoordinationOfficeSendeeService;
import com.smfx.yxt.service.ICoordinationOfficeService;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.utils.PropertiesUtil;
import com.smfx.yxt.view.CoordinationOfficeSendeeView;
import com.smfx.yxt.view.CoordinationOfficeView;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.COORDINATION_OFFICE)
@Controller
public class CoordinationOfficeController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CoordinationOfficeController.class);
	
	@Autowired
	private ICoordinationOfficeService coordinationOfficeService;
	@Autowired
	private ICoordinationOfficeSendeeService coordinationOfficeSendeeService;
	
	@RequestMapping(value = {"","/"})
	public String index(Model model) {
		model.addAttribute("relationType", RelationType.COORDINATION_OFFICE.getCode());
		return "office/index";
	}
	
	@RequestMapping("publish")
	public String publish(Model model) {
		model.addAttribute("relationType", RelationType.COORDINATION_OFFICE.getCode());
		return "office/publish";
	}
	
	@RequestMapping("myPublish")
	@ResponseBody Object myPublish(OfficeQueryVO vo) {
		try {
			AppUser appUser = getAppUser();
			Page<CoordinationOffice> page = new Page<CoordinationOffice>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			page.setSorts(sorts);
			vo.setSenderId(appUser.getTeacher().getId());
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<CoordinationOffice> list = coordinationOfficeService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---myPublish error={}", e);
		}
		return null;
	}
	
	@RequestMapping("myPublish/feedback/page")
	@ResponseBody Object myPublishFeedbackPage(OfficeQueryVO vo) {
		try {
			AppUser appUser = getAppUser();
			Page<CoordinationOfficeSendeeView> page = new Page<CoordinationOfficeSendeeView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("updateTime", "desc"));
			page.setSorts(sorts);
			vo.setSenderId(appUser.getTeacher().getId());
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<CoordinationOfficeSendeeView> list = coordinationOfficeSendeeService.selectFeedbackListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---myPublishFeedbackPage error={}", e);
		}
		return null;
	}
	
	@RequestMapping("create")
	@ResponseBody Object noticeCreate(OfficeReqVO reqVO){
		try {
			AppUser appUser = getAppUser();
			CoordinationOffice record = new CoordinationOffice();
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			record.setSchoolName(appUser.getTeacher().getSchoolName());
			record.setTermId(getCurrentTerm().getId());
			record.setTemplateCode(PropertiesUtil.getProperty("coordination_office_template"));
			record.setSenderId(appUser.getTeacher().getId());
			record.setSender(appUser.getTeacher().getName());
			record.setContent(reqVO.getContent());
			record.setWorkDate(reqVO.getWorkDate());
			coordinationOfficeService.createOffice(record, reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---noticeCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 今日已布置任务接口
	 * @param vo
	 * @return
	 */
	@RequestMapping("today")
	@ResponseBody Object todayNotice(OfficeQueryVO vo) {
		AppUser appUser = getAppUser();
		vo.setSenderId(appUser.getTeacher().getId());
		vo.setWorkDate(DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT1));
		return coordinationOfficeService.selectListByParam(vo);
	}
	
	@RequestMapping("myWork/page")
	@ResponseBody Object myWorkPage(OfficeQueryVO vo) {
		try {
			AppUser appUser = getAppUser();
			Page<CoordinationOfficeSendeeView> page = new Page<CoordinationOfficeSendeeView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("createTime", "desc"));
			sorts.add(new OrderBy("updateTime", "desc"));
			page.setSorts(sorts);
			vo.setSendeeId(appUser.getTeacher().getId());
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<CoordinationOfficeSendeeView> list = coordinationOfficeSendeeService.selectViewListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---myWorkPage error={}", e);
		}
		return null;
	}
	
	@RequestMapping("myWork/detail/{officeId}")
	public String myWorkDetail(@PathVariable Integer officeId, Model model) {
		AppUser appUser = getAppUser();
		OfficeQueryVO vo = new OfficeQueryVO();
		vo.setSendeeId(appUser.getTeacher().getId());
		vo.setOfficeId(officeId);
		CoordinationOfficeSendeeView view = coordinationOfficeSendeeService.selectViewDetail(vo);
		model.addAttribute("detail", view);
		model.addAttribute("relationType", RelationType.COORDINATION_OFFICE.getCode());
		return "office/workDetail";
	}
	
	@RequestMapping("myPublish/detail/{officeId}")
	public String myPublishDetail(@PathVariable Integer officeId, Model model) {
		AppUser appUser = getAppUser();
		OfficeQueryVO vo = new OfficeQueryVO();
		vo.setSenderId(appUser.getTeacher().getId());
		vo.setOfficeId(officeId);
		CoordinationOfficeView view = coordinationOfficeService.selectViewDetail(vo);
		model.addAttribute("detail", view);
		return "office/myPublishDetail";
	}
	
	@RequestMapping("feedback")
	@ResponseBody Object feedback(OfficeReqVO reqVO) {
		try {
			CoordinationOfficeSendee record = new CoordinationOfficeSendee();
			record.setFeedback(reqVO.getFeedback());
			record.setId(reqVO.getFeedbackId());
			record.setUpdateTime(new Date());
			coordinationOfficeSendeeService.updateByPrimaryKeySelective(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---feedback error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("finished")
	@ResponseBody Object finished(OfficeReqVO reqVO) {
		try {
			CoordinationOfficeSendee record = new CoordinationOfficeSendee();
			record.setId(reqVO.getFeedbackId());
			record.setIsFinished(1);
			record.setUpdateTime(new Date());
			coordinationOfficeSendeeService.updateByPrimaryKeySelective(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---finished error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("sendee/list")
	@ResponseBody Object sendeeList(Integer officeId) {
		return coordinationOfficeSendeeService.selectListByOfficeId(officeId);
	}
	
}
